package wowrl.gui;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.JPanel;

import wowrl.engine.WOWRL_Engine;
import wowrl.tools.ResourceLoader;

public class BoardPanel extends JPanel {

	//Store the resource loader
	private ResourceLoader	loader;
	
	//Store the loaded tilemap
	private Image tileGraphics;
	
	//Store the game engine
	WOWRL_Engine engine;

	//Location of the viewing area
	private Point viewLocation;
	
	//Size of the viewing area
	public static final int VIEW_COLS = 20; //20
	public static final int VIEW_ROWS = 15; //15
	
	public BoardPanel(ResourceLoader loader, WOWRL_Engine engine){
		this.loader = loader;
		this.engine = engine;
		
		// Load the tile graphics
		tileGraphics = loader.getResource("TileSet");
		
		// Set the size
		setSize(640,480);
		
		// Define the default view location
		viewLocation = new Point(0,0);
	}
	
	public void paintComponent(Graphics g)
	{
		viewLocation.x = engine.getPlayerLocation().x - (int) (VIEW_COLS/2) +1;
		viewLocation.y = engine.getPlayerLocation().y - (int) (VIEW_ROWS/2) +1;
		
		//Draw the appropriate tile at each location
		for (int col=0;col<VIEW_COLS;col++){
			for (int row=0;row<VIEW_ROWS;row++){
				drawTile(g,col,row,col+viewLocation.x,row+viewLocation.y);
			}
		}
	}
	
	/** Draw the highest-priority graphic at the location */
	public void drawTile(Graphics g, int xDraw, int yDraw, int xLoc, int yLoc)
	{
		//Draw level background
		//FIXME: Hardcoded
		drawTiledTerrain(g,xLoc, yLoc, xDraw,yDraw,1057);
		
		//Always draw terrain
		if (engine.isTerrainTiled(xLoc,yLoc))
		{
			drawTiledTerrain(g,xLoc, yLoc, xDraw,yDraw,engine.adjustedTerrainDisplayTileNumber(xLoc, yLoc));
		}
		else				
		{
			drawTile(g,xDraw,yDraw,engine.adjustedTerrainDisplayTileNumber(xLoc, yLoc));
			
		}
		
		//Player takes highest priority
		if (engine.playerLocation().x == xLoc && engine.playerLocation().y == yLoc)
		{
			drawTile(g,xDraw,yDraw,engine.getPlayerDisplayTileNumber());			
		}
		
		//Then other mobs
		else if (engine.getMob(xLoc,yLoc) != null)
		{
			drawTile(g,xDraw,yDraw,engine.getMob(xLoc,yLoc).displayTileNumber);		
		}
		
		//Then items
		else if (engine.getTopItem(xLoc,yLoc) != null)
		{
			drawTile(g,xDraw,yDraw,engine.getTopItem(xLoc, yLoc).displayTileNumber);		
		}
		

					
	}
	
	/** Draw the specified tileNumber at the specified coordinates */
	public void drawTile(Graphics g, int xDraw, int yDraw, int tileNumber){
		final int tileSize = 32;		
		
		int xTile = tileNumber  % 30;
		int yTile = (int) (tileNumber / 30);

		int xLocation = xDraw * tileSize;
		int yLocation = yDraw * tileSize;
		
		int xTileLocation = xTile * tileSize;
		int yTileLocation = yTile * tileSize;		
		
		g.drawImage(tileGraphics, xLocation, yLocation, xLocation+tileSize, yLocation+tileSize,
				xTileLocation, yTileLocation, xTileLocation+tileSize, yTileLocation+tileSize,this);		
	}
	
	public void drawTiledTerrain(Graphics g, int xLoc, int yLoc, int xDraw, int yDraw, int tileNumber){
		drawTile(g,xDraw,yDraw,tileNumber);
		if (((xLoc + yLoc) % 7) == 0)
		{
			drawTile(g,xDraw,yDraw,tileNumber+1);
		}
		if (((xLoc + yLoc) % 3) == 0)
		{
			drawTile(g,xDraw,yDraw,tileNumber+2);
		}
		if (((xLoc + yLoc) % 5) == 0)
		{
			drawTile(g,xDraw,yDraw,tileNumber+3);
		}
	}
}
